#include <QtCore>
//#include <QApplication>
#include "xmlutil.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QString v_ui_path = "C:/Qt/mingw-lib/test/webbrowser/mainwindow.ui";
    QFile v_ui_file(v_ui_path);
    v_ui_file.open(QIODevice::ReadOnly);
    QByteArray v_result = v_ui_file.readAll();
    qDebug() << "[v_result]" << v_ui_file.readAll();
    XmlUtil::backupXmlBody(v_result, "v_result");

    // [/ui]
    QDomDocument v_doc;
    XmlUtil::setXmlContent(v_doc, v_result);
    QDomElement v_root = v_doc.documentElement();

    // [/ui/images]
    QDomElement v_images_node = v_root.elementsByTagName("images").at(0).toElement();

    QDomNodeList v_image_list = v_images_node.elementsByTagName("image");
    qDebug() << v_image_list.count();
    for (int i=0; i<v_image_list.count(); i++)
    {
        // [/ui/images/image]
        QDomElement v_image_node = v_image_list.at(i).toElement();
        QString v_image_name = v_image_node.attribute("name");
        qDebug() << "[v_image_node]" << i << v_image_name << v_image_node.attribute("name", "[no name]");
        QDomElement v_data_node = v_image_node.elementsByTagName("data").at(0).toElement();
        //QString v_image_len = v_data_node.attribute("length");
        long v_image_len = v_data_node.attribute("length").toLong();
        QString v_image_fmt = v_data_node.attribute("format");
        QByteArray v_data_bytes;
        char *v_image_len_ptr = (char *)&v_image_len;
        v_data_bytes.append(*(v_image_len_ptr+3));
        v_data_bytes.append(*(v_image_len_ptr+2));
        v_data_bytes.append(*(v_image_len_ptr+1));
        v_data_bytes.append(*(v_image_len_ptr+0));
        v_data_bytes += QByteArray::fromHex(v_data_node.text().toLatin1());
        QByteArray v_ucomp_bytes = qUncompress(v_data_bytes);
        ////v_data_bytes = v_data_bytes + QByteArray(v_image_len-v_data_bytes.size(), (char)0);
        qDebug() << v_image_len << v_ucomp_bytes.size();
        //qDebug() << v_data_node.text();
        ////QString v_file_name = "C:/Qt/mingw-lib/test/" + v_image_name + "." + v_image_fmt;
        QString v_file_name = "C:/Qt/mingw-lib/test/webbrowser/xpm/" + v_image_name + ".xpm";
        QFile v_file(v_file_name);
        qDebug() << v_file.fileName() << v_image_len << v_data_node.text().toLatin1().size();
        v_file.open(QIODevice::WriteOnly);
        v_file.write(v_ucomp_bytes);
        v_file.close();
    }
    //Widget w;
    //w.show();
    //return a.exec();
    return 0;
}
